package com.wyh.service.serviceImpl.g3serviceimpl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.domain.dto.g3dto.BuildingEstateArrearsDto;
import com.wyh.domain.po.g3po.BuildEstateArrears;
import com.wyh.domain.po.g3po.BuildingProject;
import com.wyh.domain.vo.g3vo.BuildEstateArrearsVO;
import com.wyh.domain.vo.g3vo.BuildingProjectVO;
import com.wyh.mapper.g3mapper.BuildingProjectMapper;
import com.wyh.service.g3service.AsyncService;
import com.wyh.service.g3service.IBuildingProjectService;
import com.wyh.utils.g3utils.ProjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Service
public class BuildingProjectServiceServiceImpl extends ServiceImpl<BuildingProjectMapper, BuildingProject> implements IBuildingProjectService {
    @Autowired
    private BuildingProjectMapper buildingProjectMapper;
    @Autowired
    private AsyncService asyncService;

    private List<BuildEstateArrears> records;
    @Override
    public BuildingProjectVO selectBuildingProject() {
        BuildingProjectVO buildingProjectVO = new BuildingProjectVO();
        List<BuildingProject> buildingProjects = buildingProjectMapper.selectAllBuildingProject();
        buildingProjectVO.setBuildingProjectList(buildingProjects);
        return buildingProjectVO;
    }

    @Override
    public BuildEstateArrearsVO selectArrears(BuildingEstateArrearsDto buildingEstateArrearsDto) {
        BuildEstateArrearsVO buildEstateArrearsVO = new BuildEstateArrearsVO();
        Future<List<BuildEstateArrears>> listFuture = asyncService.selectProjectArrears(buildingEstateArrearsDto);
        Future<Long> longFuture = asyncService.selectAllTotal(buildingEstateArrearsDto);
        try {
             records = listFuture.get(300000L, TimeUnit.MILLISECONDS);
            Long total = longFuture.get(3000L, TimeUnit.MILLISECONDS);
            //封装属性值
            buildEstateArrearsVO.setRecords(records);
            buildEstateArrearsVO.setTotal(total);

        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return buildEstateArrearsVO;
    }

    @Override
    public void exportArrearsBuilding(HttpServletResponse response) {
        if (records == null) {
            throw new RuntimeException("请先查询数据");
        }
        try {
            //设置响应结果
            ProjectUtils.setExcelResponseProp(response, "楼盘欠款统计表");
            List<BuildEstateArrears> buildEstateArrears = records;
            EasyExcel.write(response.getOutputStream())
                    .head(BuildEstateArrears.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("楼盘欠款统计表")
                    .doWrite(buildEstateArrears);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



}


